<?php

require_once($GLOBALS['LNG'] . $_SESSION[$SESSLG] . '/lang.default.mex');

function GetDomainClientCampains() {
    global $sysTime, $SESSCD;
    $prStr = '';
    $GET = $GLOBALS['MYT']->db_array("SELECT cpm_ID, cpm_AffectDate FROM campaigns
            INNER JOIN campaigns_attrib ON cpm_ID = cpg_Campaign
            WHERE cpm_Appliesto = 'ClientLastRemittance' AND cpm_Flag = 2 AND cpm_Type = 2
                  AND '$sysTime' BETWEEN cpg_TStart AND cpg_TEnd
                  AND cpg_Flag = 2 AND cpg_Origen0 = ".$_SESSION[$SESSCD]);
    if (isset($GET[1])) {
        $prStr .= ' OR CliLastSend < "'.$GET[1]['cmp_AffectDate'].'"';
    }
    return $prStr;
}

$prStr = '';

$SRCLNumber = req_form($_POST['VALUE']);
$SRCLName = req_form($_POST['Name']);
$SRCLPhone = req_form($_POST['CttPhone']);
$SRCLDoc = req_form($_POST['DocID']);
$SYSMOD = $_POST['SYSMOD'];

$Qur = '';
$DomainType = '';
if (isset($sys_isagent)) {
    $DomainType = "AND CliOffice = " . $_SESSION[$SESSCD];
}
$DomainCampaigns = GetDomainClientCampains();
if ($SRCLNumber != '') {
    $Qur .= " AND CliNumero = '{$SRCLNumber}'";
}
if ($SRCLName != '' && strlen($SRCLName) > 8) {
    $SRCLName = preg_split('/ /', $SRCLName);
    $NameCt = count($SRCLName);
    for ($cl = 0; $cl < $NameCt; $cl++) {
        $Qur .= " AND CliNombre LIKE '%{$SRCLName[$cl]}%'";
    }
} elseif ($SRCLName != '') {
    $error = 'Nome muito curto';
}
if ($SRCLPhone != '' && strlen($SRCLPhone) > 5) {
    $Qur .= " AND (CliTelefono = '{$SRCLPhone}' OR CliTelefono2 = '{$SRCLPhone}')";
} elseif ($SRCLPhone != '') {
    $error = 'Telefone muito curto';
}
if ($SRCLDoc != '' && strlen($SRCLDoc) > 4) {
    $Qur .= " AND dtd_Numero LIKE '%{$SRCLDoc}%'";
} elseif ($SRCLDoc != '') {
    $error = 'Documento muito curto';
}
if (!isset($error) && $Qur == '') {
    $error = 'Parametros de consulta inválidos.';
}
if (!isset($error)) {

    $GET = $GLOBALS['MYT']->db_array("SELECT CliNumero,
                                    CliNombre,
                                    CliTelefono,
                                    CliTelefono2,
                                    dtd_Numero,
                                    CiuCiudad AS CliCiudad
                    FROM Clientes
                    LEFT OUTER JOIN clientes_ext ON CliNumero = CliExtNumero
                    LEFT OUTER JOIN ciudades ON CiuNumero = CliCiudad
                    LEFT OUTER JOIN hd_documentos ON CliNumero = dtd_Cliente AND dtd_Numero LIKE '%{$SRCLDoc}%'
                    WHERE (CliProfile > 0 $DomainType $Qur) $DomainCampaigns
                    GROUP BY CliNumero");

    $prStr .= '<div class="Alert">Deseja criar um novo cliente? ' . rt_field('button', 'ED', 'FMCL', 'Novo Cliente', '100', '38', '', 'SUBMIT" onclick="
        replaceUrl(\'contents\',\'_response/call.php?get=Operative/client\');') . '</div>
        <table width="100%" cellspacing="1">';

    if (!isset($GET[1])) {
        $prStr .= '<tr><td><div class="Line">' . $lang['error']['emptyquery'] . '</div>
	</td></tr>';
    } else {
        $prStr .= '<tr bgcolor="#BACCD1" style="height:18px">
				<td width="8%">' . $lang['basic']['number'] . '</td>
   				<td width="50%">' . $lang['basic']['name'] . '</td>
				<td NOWRAP width="10%">Telefone Fixo</td>
				<td width="10%">Telemovel</td>
				<td width="14%">' . $lang['basic']['document'] . '</td>
				<td width="18%">' . $lang['basic']['city'] . '</td>
			</tr>';
    }
    for ($s = 1; $s <= $GET[0]; $s++) {
    $prStr .= '<tr style="height:22px;" class="Selector" onMouseOver="this.className=\'Selector_Over\'"
                onMouseOut="this.className=\'Selector\'"
                onClick="replaceUrl(\'contents\',\'_response/call.php?get=Operative/client&CID=' . $GET[$s]['CliNumero'] . '&MOD=' . $SYSMOD . '\');
                this.onClick=\'\';">
                    <td style="margin:10px;" align="right">' . $GET[$s]['CliNumero'] . '&nbsp;&nbsp;</td>
                    <td>' . $GET[$s]['CliNombre'] . '</td>
                    <td>' . $GET[$s]['CliTelefono'] . '</td>
                    <td>' . $GET[$s]['CliTelefono2'] . '</td>
                    <td>' . $GET[$s]['dtd_Numero'] . '</td>
                    <td NOWRAP>' . $GET[$s]['CliCiudad'] . '</td>
                </tr>';
    }
} else {
    $prStr .= '<tr><td>' . $error . '</td></tr>';
}
$prStr .= '</table>';
echo $prStr;